<template>
  <div class="files-container">
    <!-- 搜索栏 -->
    <el-card class="search-card">
      <el-form :model="searchForm" inline>
        <el-form-item label="文件名">
          <el-input
            v-model="searchForm.fileName"
            placeholder="支持模糊搜索"
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item label="文件类型">
          <el-select v-model="searchForm.fileType" placeholder="请选择文件类型" clearable>
            <el-option label="图片" value="1" />
            <el-option label="文档" value="2" />
            <el-option label="视频" value="3" />
            <el-option label="音频" value="4" />
            <el-option label="其他" value="5" />
          </el-select>
        </el-form-item>
        <el-form-item label="用户">
          <el-input
            v-model="searchForm.nickName"
            placeholder="用户昵称"
            clearable
            @keyup.enter="handleSearch"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">
            <el-icon><Search /></el-icon>
            查询
          </el-button>
          <el-button @click="resetSearch">
            <el-icon><Refresh /></el-icon>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>

    <!-- 文件列表 -->
    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>文件列表</span>
          <div class="header-right">
            <el-button type="danger" :disabled="!selectedFiles.length" @click="handleBatchDelete">
              批量删除
            </el-button>
          </div>
        </div>
      </template>

      <el-table
        v-loading="loading"
        :data="tableData.list"
        style="width: 100%"
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" />
        <el-table-column label="文件名" min-width="200">
          <template #default="{ row }">
            <div class="file-name">
              <el-icon :size="20" class="file-icon">
                <Document v-if="row.fileType === 4 || row.fileType === 5 || row.fileType === 6 || row.fileType === 7" />
                <Picture v-else-if="row.fileType === 3" />
                <VideoPlay v-else-if="row.fileType === 1" />
                <Headset v-else-if="row.fileType === 2" />
                <Files v-else />
              </el-icon>
              <span class="name">{{ row.fileName }}</span>
            </div>
          </template>
        </el-table-column>
        <el-table-column prop="fileSize" label="大小" width="120">
          <template #default="{ row }">
            {{ proxy.Utils.size2Str(row.fileSize) }}
          </template>
        </el-table-column>
        <el-table-column label="类型" width="100">
          <template #default="{ row }">
            <el-tag :type="getFileTypeTag(row.fileType)">
              {{ getFileTypeName(row.fileType) }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="nickName" label="所属用户" width="120" />
        <el-table-column prop="createTime" label="上传时间" width="180" />
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="{ row }">
            <el-button
              link
              type="primary"
              @click="download(row)"
            >
              下载
            </el-button>
            <el-button
              link
              type="danger"
              @click="handleDelete(row)"
            >
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="tableData.pageNo"
          v-model:page-size="tableData.pageSize"
          :total="tableData.totalCount"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup>
import { ref, getCurrentInstance } from 'vue'
import { Search, Refresh, Document, Picture, VideoPlay, Headset, Files } from '@element-plus/icons-vue'

const { proxy } = getCurrentInstance()

const api = {
  loadFileList: '/admin/loadFileList',
  deleteFile: '/admin/delFile',
  createDownloadUrl: '/admin/createDownloadUrl',
  download: '/api/admin/download'
}

// 搜索表单
const searchForm = ref({
  fileName: '',
  fileType: '',
  nickName: ''
})

// 表格数据
const loading = ref(false)
const tableData = ref({
  pageNo: 1,
  pageSize: 10,
  totalCount: 0,
  list: []
})

// 选中的文件
const selectedFiles = ref([])

// 加载文件列表
const loadFileList = async () => {
  loading.value = true
  try {
    const result = await proxy.Request({
      url: api.loadFileList,
      params: {
        pageNo: tableData.value.pageNo,
        pageSize: tableData.value.pageSize,
        ...searchForm.value
      }
    })
    if (result) {
      tableData.value = result.data
    }
  } finally {
    loading.value = false
  }
}

// 搜索
const handleSearch = () => {
  tableData.value.pageNo = 1
  loadFileList()
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {}
  handleSearch()
}

// 分页
const handleSizeChange = (val) => {
  tableData.value.pageSize = val
  loadFileList()
}

const handleCurrentChange = (val) => {
  tableData.value.pageNo = val
  loadFileList()
}

// 文件类型
const getFileTypeName = (type) => {
  const types = {
    1: '视频',
    2: '音频',
    3: '图片',
    4: 'PDF',
    5: 'Word',
    6: 'Excel',
    7: 'TXT',
    8: '代码',
    9: '压缩包',
    10: '其他'
  }
  return types[type] || '其他'
}

const getFileTypeTag = (type) => {
  const types = {
    1: 'warning',   // 视频
    2: 'info',      // 音频
    3: 'success',   // 图片
    4: 'primary',   // PDF
    5: 'primary',   // Word
    6: 'primary',   // Excel
    7: 'primary',   // TXT
    8: 'danger',    // 代码
    9: 'info',      // 压缩包
    10: ''          // 其他
  }
  return types[type] || ''
}

// 选择文件
const handleSelectionChange = (selection) => {
  selectedFiles.value = selection
}

// 下载文件
const download = async (row) => {
  let result = await proxy.Request({
    url: api.createDownloadUrl + "/" + row.userId + "/" + row.fileId
  });
  if (!result) {
    return;
  }
  window.location.href = api.download + "/" + result.data;
};

// 删除单个文件
const handleDelete = (row) => {
  proxy.Confirm('确定要删除该文件吗？删除后不可恢复', async () => {
    const result = await proxy.Request({
      url: api.deleteFile,
      params: {
        fileIdAndUserIds: row.userId + "_" + row.fileId
      }
    })
    if (result) {
      proxy.Message.success('删除成功')
      loadFileList()
    }
  })
}

// 批量删除
const handleBatchDelete = () => {
  if (!selectedFiles.value.length) {
    proxy.Message.warning('请选择要删除的文件')
    return
  }

  proxy.Confirm(`确定要删除选中的 ${selectedFiles.value.length} 个文件吗？删除后不可恢复`, async () => {
    const fileIdAndUserIds = selectedFiles.value.map(file => file.userId + "_" + file.fileId).join(",")
    const result = await proxy.Request({
      url: api.deleteFile,
      params: {
        fileIdAndUserIds: fileIdAndUserIds
      }
    })
    if (result) {
      proxy.Message.success('批量删除成功')
      loadFileList()
    }
  })
}

// 初始加载
loadFileList()
</script>

<style lang="scss" scoped>
.files-container {
  .search-card {
    margin-bottom: 20px;
    border: none;
    background: linear-gradient(to right, #fff, #fafafa);
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
  }
  
  .table-card {
    border: none;
    background: linear-gradient(to right, #fff, #fafafa);
    transition: all 0.3s;
    
    &:hover {
      transform: translateY(-4px);
      box-shadow: 0 4px 12px rgba(0,0,0,0.1);
    }
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 16px 20px;
      border-bottom: 1px solid #f0f0f0;
      
      span {
        font-size: 16px;
        font-weight: bold;
        background: linear-gradient(120deg, #bd34fe 30%, #47caff);
        -webkit-background-clip: text;
        -webkit-text-fill-color: transparent;
      }
      
      .header-right {
        .el-button {
          &.el-button--danger {
            background: linear-gradient(120deg, #ff4d4f 30%, #ff7875);
            border: none;
            padding: 8px 16px;
            transition: all 0.3s;
            
            &:hover {
              transform: translateY(-2px);
              box-shadow: 0 4px 12px rgba(255, 77, 79, 0.3);
            }
            
            &:active {
              transform: translateY(0);
            }
            
            &.is-disabled {
              background: #f5f5f5;
              color: #00000040;
              box-shadow: none;
              
              &:hover {
                transform: none;
              }
            }
          }
        }
      }
    }
    
    :deep(.el-table) {
      background: transparent;
      
      th {
        background-color: #fafafa;
        font-weight: bold;
        color: #606266;
        
        &.is-leaf {
          border-bottom: 2px solid #EBEEF5;
        }
      }
      
      td {
        padding: 12px 0;
      }
      
      .file-name {
        display: flex;
        align-items: center;
        
        .file-icon {
          margin-right: 8px;
          transition: all 0.3s;
          
          &.el-icon {
            font-size: 20px;
            
            &[class*="Document"] {
              color: #409EFF;
            }
            
            &[class*="Picture"] {
              color: #67C23A;
            }
            
            &[class*="VideoPlay"] {
              color: #E6A23C;
            }
            
            &[class*="Headset"] {
              color: #909399;
            }
            
            &[class*="Files"] {
              color: #909399;
            }
          }
        }
        
        .name {
          color: #303133;
          transition: all 0.3s;
          
          &:hover {
            color: #409EFF;
          }
        }
      }
      
      .el-button {
        padding: 6px 0;
        font-weight: normal;
        
        &.el-button--primary {
          color: #409EFF;
          
          &:hover {
            color: #66b1ff;
          }
        }
        
        &.el-button--danger {
          color: #F56C6C;
          
          &:hover {
            color: #f78989;
          }
        }
      }
    }
    
    .pagination-container {
      margin-top: 20px;
      padding: 10px 20px;
      display: flex;
      justify-content: flex-end;
      background-color: #fafafa;
      border-top: 1px solid #f0f0f0;
    }
  }
}

// 标签样式
:deep(.el-tag) {
  border-radius: 4px;
  padding: 0 8px;
  height: 24px;
  line-height: 22px;
  
  &.el-tag--success {
    background-color: rgba(103, 194, 58, 0.1);
    border-color: rgba(103, 194, 58, 0.2);
    color: #67c23a;
  }
  
  &.el-tag--primary {
    background-color: rgba(64, 158, 255, 0.1);
    border-color: rgba(64, 158, 255, 0.2);
    color: #409eff;
  }
  
  &.el-tag--warning {
    background-color: rgba(230, 162, 60, 0.1);
    border-color: rgba(230, 162, 60, 0.2);
    color: #e6a23c;
  }
  
  &.el-tag--info {
    background-color: rgba(144, 147, 153, 0.1);
    border-color: rgba(144, 147, 153, 0.2);
    color: #909399;
  }
}

// 响应式布局
@media screen and (max-width: 768px) {
  .files-container {
    .search-card {
      .el-form-item {
        margin-right: 0;
        margin-bottom: 16px;
        
        &:last-child {
          margin-bottom: 0;
        }
      }
    }
    
    .table-card {
      :deep(.el-table) {
        .file-name {
          .name {
            max-width: 120px;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
          }
        }
        
        .el-button + .el-button {
          margin-left: 8px;
        }
      }
    }
  }
}
</style> 